package edu.vt.sdm.tcga.filenavigator.parser.clinical;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import edu.vt.sdm.tcga.filenavigator.parser.HibernateUtil;

public class ClinicalSlideTable {

	private List<ClinicalSlide> values;

	// Accessor methods
	public List<ClinicalSlide> getValues() {
		return values;
	}
	public void setValues(List<ClinicalSlide> values) {
		this.values = values;
	}
	
	
	// Constructors
	public ClinicalSlideTable(){
		values = new ArrayList<ClinicalSlide>();
	}
	
	public ClinicalSlideTable(String filename){
		this();
		this.loadFromFile(filename);
	}
	
	
	// Utility functions
	public void loadFromFile(String filename){
		
		// Get current time
		long start = System.currentTimeMillis();
		
		System.out.println("Attempting to load clinical slide file: " + filename);
		
		try {
			BufferedReader in  = new BufferedReader(new FileReader(filename));
			String line = "";
			String delims = "\t";
	
			// Load Analyses
			// Skip the header line in the file.
			line = in.readLine();

			while( (line = in.readLine()) != null){
				String[] tokens = line.split(delims);

				String bcrSampleBarcode = tokens[0];
				String bcrSlideBarcode = tokens[1];
				String numberProliferatingCells = tokens[2];
				String percentEosinophilInfiltration = tokens[3];
				String percentGranulocyteInfiltration = tokens[4];
				String percentInflamInfiltration = tokens[5];
				String percentLymphocyteInfiltration = tokens[6];
				String percentMonocyteInfiltration = tokens[7];
				String percentNecrosis = tokens[8];
				String percentNeutrophilInfiltration = tokens[9];
				String percentNormalCells = tokens[10];
				String percentStromalCells = tokens[11];
				String percentTumorCells = tokens[12];
				String percentTumorNuclei = tokens[13];
				String sectionLocation = tokens[14];
				
				ClinicalSlide clinical = new ClinicalSlide(bcrSampleBarcode,
						bcrSlideBarcode,
						numberProliferatingCells,
						percentEosinophilInfiltration,
						percentGranulocyteInfiltration,
						percentInflamInfiltration,
						percentLymphocyteInfiltration,
						percentMonocyteInfiltration, 
						percentNecrosis,
						percentNeutrophilInfiltration, 
						percentNormalCells,
						percentStromalCells, 
						percentTumorCells,
						percentTumorNuclei, 
						sectionLocation);
				
				values.add(clinical);
			}
			
		}
		catch (FileNotFoundException e) {
			e.printStackTrace();
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
		
		// Get elapsed time in milliseconds
		long elapsedTimeMillis = System.currentTimeMillis()-start;
		double seconds = elapsedTimeMillis / 1000.0;
		
		System.out.println("Loaded clinical slide data in " + seconds + " seconds");
	}
	
	// Save the class out to the database
	public void saveToDatabase(){
		
		// Get current time
		long start = System.currentTimeMillis();
				
		try{
			Session session = HibernateUtil.getSession();
	        Transaction tx = session.beginTransaction();
	        int i = 0;
	        
	        for(ClinicalSlide clinical : this.getValues()){
	        	session.save(clinical);
		        
	            if ( i % 20 == 0 ) { //Same as the JDBC batch size
	                //flush a batch of inserts and release memory:
	                session.flush();
	                session.clear();
	            }
	            
		        i++;
	        }
	        
	        tx.commit();
	        session.close();
		}
		catch(Exception e){
			e.printStackTrace();
		}

		// Get elapsed time in milliseconds
		long elapsedTimeMillis = System.currentTimeMillis()-start;
		double seconds = elapsedTimeMillis / 1000.0;
		
		System.out.println("Saved clinical slide data in " + seconds + " seconds");
	}
}
